package com.ftg.learn.chapter02.ee.service.impl;

import com.ftg.learn.chapter02.ee.domain.SysDept;
import com.ftg.learn.chapter02.ee.mapper.SysDeptMapper;
import com.ftg.learn.chapter02.ee.service.SysDeptService;
import com.ftg.learn.chapter02.ee.util.MybatisComm;
import com.ftg.learn.chapter02.ee.util.vo.PageVo;
import com.ftg.learn.chapter02.ee.util.web.R;
import com.ftg.learn.chapter02.ee.vo.SysUserDeptVo;

import java.util.List;
import java.util.Objects;

/**
 * 实现类 -
 *
 * @author kangjx
 * @version 1.0
 * @date 2024/1/2 上午9:06
 */
public class SysDeptServiceImpl implements SysDeptService {


    private MybatisComm mybatisComm = new MybatisComm(new String[]{
            "ee/sysuser.xml", "ee/sysdept.xml"
    }, "ee/jdbc.properties");


    @Override
    public R<Integer> addDept(SysDept dept) {
        SysDeptMapper sysDeptMapper = mybatisComm.getInterfaceMapper(SysDeptMapper.class);
        try {
            assert Objects.nonNull(dept);
            int n = 0;
            if (sysDeptMapper.getCountByDname(dept.getDname()) == 0) {
                n = sysDeptMapper.saveDept(dept);
                System.out.println("nnnn--->" + n);
                mybatisComm.commitSqlsession();
            }
            return R.ok(n);
        } catch (RuntimeException s) {
            mybatisComm.rollbackSqlsession();
            return R.failure();
        } finally {
            mybatisComm.closeSqlSession();
        }

    }

    @Override
    public R<List<SysUserDeptVo>> showDeptUser() {
        SysDeptMapper sysDeptMapper = mybatisComm.getInterfaceMapper(SysDeptMapper.class);
        try {
            List<SysUserDeptVo> list = sysDeptMapper.showDeptUser();
            return R.ok(list);

        } catch (RuntimeException s) {
            mybatisComm.rollbackSqlsession();
            return R.failure();
        } finally {
            mybatisComm.closeSqlSession();
        }
    }

    @Override
    public R<Integer> logicDeleteById(Integer id) {
        SysDeptMapper sysDeptMapper = mybatisComm.getInterfaceMapper(SysDeptMapper.class);
        try {
            int n = sysDeptMapper.logicDeleteById(id);
            mybatisComm.commitSqlsession();
            return R.ok(n);
        } catch (RuntimeException s) {
            mybatisComm.rollbackSqlsession();
            return R.failure();
        } finally {
            mybatisComm.closeSqlSession();
        }
    }

    @Override
    public R<PageVo<SysDept>> list(String dname, Integer statue, Long pageNum, Long pageSize) {
        PageVo<SysDept> page = new PageVo<>();
        SysDeptMapper sysDeptMapper = mybatisComm.getInterfaceMapper(SysDeptMapper.class);
        try {
            page.setPageNum(pageNum);

            page.setPageSize(pageSize);

            pageNum = pageNum <= 0 ? 1 : pageNum;
            page.setRows(sysDeptMapper.list(dname, statue, (pageNum - 1) * pageSize, pageSize));


            page.setTotal(sysDeptMapper.countByNameAndStatue(dname, statue));
            return R.ok(page);
        } catch (RuntimeException s) {
            mybatisComm.rollbackSqlsession();
            return R.failure();
        } finally {
            mybatisComm.closeSqlSession();
        }
    }
}
